package com.dycx.food.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.food.dto.SpecValueDTO;
import com.dycx.food.entity.SpecValueDO;
import com.dycx.food.mapper.SpecValueFoodMapper;
import com.dycx.food.service.SpecValueFoodService;
import com.dycx.food.vo.SpecValueVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;


@Service
@AllArgsConstructor
public class SpecValueFoodImpl extends ServiceImpl<SpecValueFoodMapper, SpecValueDO> implements SpecValueFoodService {
    private SpecValueFoodMapper specValueMapper;

    @Override
    public Boolean delete(Integer specValueId) {
        return this.removeById(specValueId);
    }

    /**
     * 添加
     *
     * @param specValueDto
     * @return
     */
    @Override
    public Integer addOneSpecValue(SpecValueDTO specValueDto) {
        LambdaQueryWrapper<SpecValueDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpecValueDO::getSpecId, specValueDto.getSpecId());
        queryWrapper.eq(SpecValueDO::getSpecValue, specValueDto.getSpecValue());

        int num = this.count(queryWrapper);

        if (num > 0) {
            throw new RuntimeException("已存在相同的规格值，请勿重复添加");
        }

        SpecValueDO specValueDO = BeanUtil.toBean(specValueDto, SpecValueDO.class);
        specValueDO.setSort(specValueMapper.getMaxSort());

        this.save(specValueDO);

        return specValueDO.getSpecValueId();
    }

    /**
     * 添加/修改
     *
     * @param specValueDto
     * @return
     */
    @Override
    public Boolean update(SpecValueDTO specValueDto) {
        SpecValueDO specValueDO = BeanUtil.toBean(specValueDto, SpecValueDO.class);
        return this.updateById(specValueDO);
    }


    /**
     * 获取所有列表
     *
     * @return
     */
    @Override
    public List<SpecValueVO> getAllListBySpecId(int specId) {
        LambdaQueryWrapper<SpecValueDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpecValueDO::getSpecId, specId);
        queryWrapper.orderByAsc(SpecValueDO::getSort);

        List<SpecValueDO> specValueDOList = this.list(queryWrapper);

        return specValueDOList.stream().map(item ->
                BeanUtil.toBean(item, SpecValueVO.class)
        ).collect(Collectors.toList());
    }

    @Override
    public Boolean saveSpecValue(List<SpecValueDTO> list) {

        Assert.notEmpty(list, "至少添加一个值");

        LambdaQueryWrapper<SpecValueDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpecValueDO::getSpecId, list.get(0).getSpecId());

        List<SpecValueDO> originSpecValueList = this.list(queryWrapper);

        //删除
        List<Integer> idList = new ArrayList<>();
        originSpecValueList.forEach(item -> {
            AtomicBoolean isInc = new AtomicBoolean(false);
            list.forEach(value -> {
                if (value.getSpecValueId().equals(item.getSpecValueId())) {
                    isInc.set(true);
                }
            });

            if (!isInc.get()) {
                idList.add(item.getSpecValueId());
            }
        });

        if (idList.size() > 0) {
            this.removeByIds(idList);
        }

        //新增和修改
        for (int i = 0; i < list.size(); i++) {
            SpecValueDO specValueDO = BeanUtil.toBean(list.get(i), SpecValueDO.class);
            if (!specValueDO.getSpecValue().equals("")) {
                specValueDO.setSort(i);
                if (list.get(i).getSpecValueId() == 0) {
                    this.save(specValueDO);
                } else {
                    this.updateById(specValueDO);
                }
            }
        }

        return true;
    }

}
